package top.codecrab.eblog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.stereotype.Service;
import top.codecrab.eblog.entity.UserMessage;
import top.codecrab.eblog.mapper.UserMessageMapper;
import top.codecrab.eblog.service.WebSocketService;

/**
 * @author codecrab
 */
@Service
public class WebSocketServiceImpl implements WebSocketService {

    @Autowired
    private SimpMessagingTemplate messagingTemplate;

    @Autowired
    private UserMessageMapper messageMapper;

    @Override
    public void sendNotReadCountToUser(Long toUserId) {
        Long count = messageMapper.selectCount(
            new QueryWrapper<UserMessage>()
                .eq("to_user_id", toUserId)
                .eq("status", 0)
        );

        //websocket通知 通道==> /user/1/messageCount
        messagingTemplate.convertAndSendToUser(toUserId.toString(), "/messageCount", count);
    }
}
